#!/bin/bash
#Git Post Merge Hook
#---------------------
#Gets the latest tag info from the git repo and updates the AssemblyInfo.cs file with it.
#This file needs to be place in the .git/hooks/ folder and only works when a git pull is
#made which contains changes in the remote repo.

#get the latest tag info. The 'always' flag will give you a shortened SHA1 if no tag exists.
tag=$(git describe --always)
echo $tag

#If no tag has been added only the sha1 will be returned
if [[ $tag=="*.*" ]]
then
	IFS='.' read -ra TAG <<< "$tag"
	#echo "${TAG[0]}"
	#echo "${TAG[1]}"

	IFS='-' read -ra COMMITS <<< "${TAG[2]}"
	#echo "${COMMITS[0]}"
	#echo "${COMMITS[1]}"
	#echo "${COMMITS[2]}"

	#This will be the version in the format <major>.<minor>.<build number>.<revision>
	version="${TAG[0]}"."${TAG[1]}"."${COMMITS[0]}"."${COMMITS[1]}"
	echo $version

	#Update the AssemblyVersion and AssemblyFileVersion attribute with the 'version'
	sed -i.bak "s/\AssemblyVersion(\".*\")/AssemblyVersion(\"$version\")/g" src/Properties/AssemblyInfo.cs
	sed -i.bak "s/\AssemblyFileVersion(\".*\")/AssemblyFileVersion(\"$version\")/g" src/Properties/AssemblyInfo.cs
fi

#This swaps the AssemblyInformationalVersion attribute with the new git describe info
sed -i.bak "s/\AssemblyInformationalVersion(\".*\")/AssemblyInformationalVersion(\"$tag\")/g" src/Properties/AssemblyInfo.cs

#cat src/Properties/AssemblyInfo.cs